<?php

class department_IndexController extends Zend_Controller_Action
{
public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	
    	
	}
	public function listAction()
	{
		$db = new department_Model_DbTable_DbDepartment();
		//print_r($data);exit();
		$this->_helper->layout()->disableLayout();
		if($this->getRequest()->isPost()){	
			$is = $this->getRequest()->getPost();
			//print_r($is);exit();
			if($this->getRequest()->getParam("edit")){
				$id = $this->getRequest()->getParam("chk_modify");
				//print_r($is);exit();
				if(!empty($id)){
					$this->_redirect("/department/index/edit/id/".$id);
					$frm = new department_Form_FrmDpt();
					$row = $db->getpdt($id);
					$dbdpt = $frm->fromdpt($row);
					$this->view->fromdepartment= $dbdpt;
				}
				else {
					echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
				}
			}
			if($this->getRequest()->getParam("delete")){
				$id = $this->getRequest()->getParam("chk_modify");
				//print_r($id); exit;
				if(!empty($id)){
					$db->deletepdt($id);
					$this->_redirect("department/index/list");
					echo "<script type='text/javascript'> alert('Delete Department Successfull'); </script>";
				}
				else {
					echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
				}
			}
			if($this->getRequest()->getPost("status")){
				$id = $this->getRequest()->getParam("status");
				//print_r($id);exit();
				if(!empty($id)){
					$dbdpt = $db->updateUnStatus ($id);
					//echo "<script type='text/javascript'> alert('Update Status Department Successfull'); </script>";
				}
				//$this->_redirect("faculty/fac/index");
			}
			if($this->getRequest()->getPost("unstatus")){
				$id = $this->getRequest()->getParam("unstatus");
				//print_r($id);exit();
				$dbdpt = $db->updateStatus($id);
				//echo "<script type='text/javascript'> alert('Update Status Department Successfull'); </script>";
			}
		}  
		$data = $db->selectdpt();
		$this->view->fromdpt = $data;
	}
	public function addAction(){
		//$this->_helper->layout()->disableLayout();
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$db = new department_Model_DbTable_DbDepartment();
			if($this->getRequest()->getParam("save_colse")){
				if($this->getRequest()->getParam("dpt_name") !=""){
					$db->insertdp($data);
					echo "<script type='text/javascript'> alert('Insert Department Successfull'); </script>";
					$this->_redirect("/department/");
				}
				else {
					echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
				}
			}
			elseif($this->getRequest()->getParam("save_new")){
				if($this->getRequest()->getParam("dpt_name") !=""){
					$db->insertdp($data);
					echo "<script type='text/javascript'> alert('Insert Department Successfull'); </script>";
					$this->_redirect("/department/index/add");
				}
				else {
					echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
				}
			}
			elseif ($this->getRequest()->getParam("cancel")) {
				$this->_redirect("/department/index/list");
			}
		}
		$frmdpt = new department_Form_FrmDpt();
		$frmadd = $frmdpt->fromdpt();
		$this->view->fromdepartment= $frmadd;
	}
	public function editAction()
	{
		//$this->_helper->layout()->disableLayout();		
		$db = new department_Model_DbTable_DbDepartment();
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			//print_r($data);
			if($this->getRequest()->getParam("save_colse")){
				$row = $db->updatedpt($data);
				$this->_redirect("/department/");
			}
			elseif($this->getRequest()->getParam("save_new")){
				$row = $db->updatedpt($data);
				$this->_redirect("/department/index/add");
			}
			elseif ($this->getRequest()->getParam("save")){
				$row = $db->updatedpt($data);
				$id = $this->getRequest()->getParam("id");
				$rows = $db->getpdt($id);
				$frmdpt = new department_Form_FrmDpt();
				$frmedit = $frmdpt->fromdpt($rows);
				$this->view->fromdepartment= $frmedit;
				echo "<script type='text/javascript'> alert('Update Status Department Successfull'); </script>";
			}
			elseif ($this->getRequest()->getParam("cancel")) {
				//print_r($data);exit();
				$this->_redirect("/department/index/list");
			}
		}				
		$id = $this->getRequest()->getParam("id");
		$rows = $db->getpdt($id);
		//print_r($rows);exit();
		$frmdpt = new department_Form_FrmDpt();
		$frmedit = $frmdpt->fromdpt($rows);
		$this->view->fromdepartment= $frmedit;
		
	}
	public function deleteAction(){
		$db = new department_Model_DbTable_DbDepartment();
		$id = $this->getRequest()->getParam("id");
		$db->deletepdt($id);
		$this->_redirect("/department/index/list");
	}
}